package com.essc.serviceImpl;

import com.essc.mapper.AdminMapper;
import com.essc.pojo.Goods;
import com.essc.pojo.User;
import com.essc.service.AdminService;
import com.essc.utils.JsonResult;
import com.essc.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.jasper.compiler.JspUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired(required = false)
    AdminMapper adminMapper;


    //查询所有用户信息
    @Override
    public JsonResult findAllUserInfoAdmin() {
        //获取全部用户信息
        List<User> userList = adminMapper.findAllUserAdmin();
        return new JsonResult(200,"用户信息",userList);
    }
    /*@Override
    public JsonResult findAllUserInfo(Integer pageNum,HttpSession session) {
        pageNum = pageNum + 1;
        int pageSize = 4;
        //获取所有记录总数
        int allCount = userMapper.findAllUserCount();
        //获取总页数
        int totalPage = allCount%pageSize!=0 ? allCount/pageSize+1:allCount/pageSize;
        if (pageNum >= totalPage){
            pageNum = totalPage;
        }
        //将总页数返回到页面
        session.setAttribute("totalPage",totalPage);
        //将当前页面数传到页面
        session.setAttribute("pageNum",pageNum);
        //设置分页
        PageHelper.startPage(pageNum,pageSize);
        //获取全部用户信息
        List<User> userList = userMapper.findAllUser();
        //分页
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        List<User> userList1 = userPageInfo.getList();
        return new JsonResult(200,"用户信息",userList1);
    }*/

    //管理员_新增用户——检查用户名
    /*@Override
    public JsonResult checkUserName(String username) {
        if (username==null||username==""){
            return new JsonResult(201,"用户名必填");
        }
        List<User> userList = userMapper.findAllUser();
        for (User user:userList){
            if (username.equals(user.getUsername())){
                return new JsonResult(202,"用户名已存在");
            }
        }
        return new JsonResult(200,"用户名可用");
    }*/

    //管理员_新增用户——检查密码
    /*@Override
    public JsonResult checkPassword(String password) {
        if (password==null||password==""){
            return new JsonResult(201,"密码必填");
        }
       return new JsonResult(200,"密码可用");
    }*/

    //管理员———修改用户信息
    @Override
    public JsonResult updateUserMessageAdmin(User user) {
        int row = adminMapper.updateUserMessageAdmin(user);
        if (row != 1){
            return new JsonResult(203,"修改失败");
        }
        return new JsonResult(200,"修改成功");
    }

    //管理员_删除用户 根据uid删除用户信息————软删除，其实是修改用户状态为0；
    @Override
    public JsonResult deleteUserInfoByUidAdmin(Integer uid) {
        if (uid==null){
            return new JsonResult(201,"会话超时，请重新登录");
        }
        int row = adminMapper.deleteUserInfoByUidAdmin(uid);
        if (row != 1){
            return new JsonResult(202,"删除失败");
        }
        return new JsonResult(200,"删除成功");
    }

    //管理员_删除用户 根据uid批量删除用户信息————软删除，其实是修改用户状态为0；
    @Override
    public JsonResult deleteUserInfoByUidArrayAdmin(Integer[] uidArray) {
        for (Integer uid:uidArray){
            if (uid==null){
                return new JsonResult(201,"会话超时，请重新登录");
            }
            int row = adminMapper.deleteUserInfoByUidAdmin(uid);
            if (row != 1){
                return new JsonResult(202,"删除失败");
            }
        }
        return new JsonResult(200,"删除成功");
    }


    //搜索页——分页——根据pageNum和pageSize查询用户信息
    @Override
    public JsonResult findUserInfoByPageAdmin(Integer pageNum, Integer pageSize) {
        //设置分页
        PageHelper.startPage(pageNum,pageSize);
        //获取所有用户数据
        List<User> userList = adminMapper.findAllUserAdmin();
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        int totalSize = (int) userPageInfo.getTotal();
        List<User> list = userPageInfo.getList();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,list);
        return new JsonResult(200,pageResult);
    }

    //搜索页操作————根据pageNum查询对应的用户数据，pageSize值写死为4，返回到userList.jsp中
    /*@Override
    public String findUserInfoByPageNum(Integer pageNum,HttpSession session) {
        Integer pageSize = 4;
        //设置分页
        PageHelper.startPage(pageNum,pageSize);
        //获取所有用户数据
        List<User> userList = userMapper.findAllUser();
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        List<User> list = userPageInfo.getList();
        session.setAttribute("userList",list);
        return "userList";
    }*/



    //管理员_新增用户
    @Override
    public JsonResult addUserAdmin(User user) {
        if (user.getUsername()==null||user.getUsername()==""){
            return new JsonResult(201,"用户名必填");
        }
        List<User> userList = adminMapper.findAllUserAdmin();
        for (User user1:userList){
            if (user.getUsername().equals(user1.getUsername())){
                return new JsonResult(202,"用户名已存在");
            }
        }
        if (user.getPassword()==null||user.getPassword()==""){
            return new JsonResult(204,"密码必填");
        }
        //生成一个盐
        String salt = UUID.randomUUID().toString();
        //获取输入的原密码
        String password = user.getPassword();
        //对原密码进行加密，加密后的密码
        String md5Pwd = getMD5Pwd(password,salt);
        //将加密后的密码设置到User对象中
        user.setPassword(md5Pwd);
        user.setSalt(salt);
        int row = adminMapper.addUserAdmin(user);
        if (row != 1){
            return new JsonResult(203,"添加失败");
        }
        return new JsonResult(200,"添加成功");
    }

    //封装一个方法，对密码进行加密
    private String getMD5Pwd(String password,String salt){
        String md5Pwd = password+salt;
        //加密
        for (int i=0; i<10; i++){
            md5Pwd = DigestUtils.md5DigestAsHex(md5Pwd.getBytes());
        }
        return md5Pwd;
    }

    //===========俊龙 7.10新增===================
    //管理员-用户管理-根据用户id查找用户信息
    @Override
    public JsonResult findUserInfoByUidAdmin(Integer uid) {
        User user = adminMapper.findUserInfoByUidAdmin(uid);
        if (user==null){
            return new JsonResult(201,"查询失败");
        }
        return new JsonResult(200,"查询成功",user);
    }

    //管理员-商品管理-查询所有商品信息
    @Override
    public JsonResult findAllGoodsInfoAdmin(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = adminMapper.findAllGoodsInfoAdmin();
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        int totalSize = (int)pageInfo.getTotal();
        List<Goods> goodsList1 = pageInfo.getList();
        PageResult pageResult = new PageResult(pageNum,pageSize,totalSize,goodsList1);
        return new JsonResult(200,"查询成功",pageResult);
    }

    //管理员-商品管理-根据商品id查询商品信息
    @Override
    public JsonResult findGoodsInfoByIdAdmin(Integer gid) {
        Goods goods = adminMapper.findGoodsInfoByIdAdmin(gid);
        return new JsonResult(200,"查询成功",goods);
    }

    //管理员-商品管理-添加商品
    @Override
    public JsonResult addGoodsInfoAdmin(Goods goods) {
        int row = adminMapper.addGoodInfoAdmin(goods);
        if (row != 1){
            return new JsonResult(201,"添加失败");
        }
        return new JsonResult(200,"添加成功");
    }

    //管理员-商品管理-删除商品
    @Override
    public JsonResult deleteGoodsInfoAdmin(Integer gid) {
        int row = adminMapper.deleteGoodsInfoAdmin(gid);
        if (row != 1){
            return new JsonResult(201,"删除失败");
        }
        return new JsonResult(200,"删除成功");
    }

    //管理员-商品管理-修改商品信息
    @Override
    public JsonResult updateGoodsInfoAdmin(Goods goods) {
        int row = adminMapper.updateGoodsInfoAdmin(goods);
        if (row != 1){
            return new JsonResult(201,"修改失败");
        }
        return new JsonResult(200,"修改成功");
    }
}
